<?php
namespace Common\Controller;
use Think\Controller;

class BaseController extends Controller {


    // 当前登陆用户基本信息
    public static $u = array();

    protected function _initialize()
    {
        $this->assign('controller', CONTROLLER_NAME);
        $this->assign('model', MODEL_NAME);
        $this->assign('action', ACTION_NAME);
        $this->assign('site_url', C("site_url"));

        //登陆用户信息
        $where = array("username"=>session("username"));
        $user = M("user u")
            ->field("u.*, r.name as role_name, r.code as role_code")
            ->where($where)
            ->join(C('DB_PREFIX')."role r on r.id=u.role_id", "LEFT")
            ->find();
        $this->assign('u', $user);
        self::$u = $user;

        //权限代码
        $rightsCode = M("rassign a")
            ->field("r.code")
            ->where(array('a.role_id'=>$user['role_id']))
            ->join(C('DB_PREFIX')."rights r on r.id=a.rights_id", "LEFT")
            ->select();
        $code = array();
        foreach ($rightsCode as $item) {
            $code[] = $item['code'];
        }
        $this->assign('rightsCode', $code);
    }

    public static function IsDateValid($date, $format = "Y/m/d") {
        $unixTime = strtotime($date);
        if (!$unixTime) { //strtotime转换不对，日期格式显然不对。
            return false;
        }
        //校验日期的有效性，只要满足其中一个格式就OK
        if (date($format, $unixTime) == $date) {
            return true;
        }
        return false;
    }

}